/* 
 -----------------------------------------------------------------------------
                   Cogaen - Component-based Game Engine (v3)
 -----------------------------------------------------------------------------
 This software is developed by the Cogaen Development Team. Please have a 
 look at our project home page for further details: http://www.cogaen.org
    
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 Copyright (c) 2010 Roman Divotkey

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/

package org.cogaen.input;

import org.cogaen.core.Core;
import org.cogaen.event.Event;
import org.cogaen.event.EventListener;
import org.cogaen.event.EventManager;
import org.cogaen.event.EventType;

public class TwoAxisController implements EventListener {

	private double hPos;
	private double vPos;
	private boolean actions[];
	private Core core;
	private String name;

	public TwoAxisController(Core core, String name, int numOfActions) {
		this.core = core;
		this.name = name;
		this.actions = new boolean[numOfActions];
	}

	public void engage() {
		EventManager.getInstance(this.core).addListener(this, UpdateEvent.TYPE);
	}
	
	public void disengage() {
		EventManager.getInstance(this.core).removeListener(this);
	}
	
	public double getHorizontalPosition() {
		return hPos;
	}
	
	public double getVerticalPosition() {
		return vPos;
	}
	
	public boolean isAction(int idx) {
		return this.actions[idx];
	}
			
	@Override
	public void handleEvent(Event event) {
		if (event.getType().equals(UpdateEvent.TYPE)) {
			UpdateEvent update = (UpdateEvent) event;
			if (update.getName() == this.name) {
				update.updateController(this);
			}
		}
	}
	
	public static abstract class UpdateEvent extends Event {
		
		public static final EventType TYPE = new EventType("TwoAxisControllerUpdate");
		
		private String name;

		public UpdateEvent(String name) {
			this.name = name;
		}
		
		public String getName()  {
			return this.name;
		}
		
		@Override
		public EventType getType() {
			return TYPE;
		}
		
		protected abstract void updateController(TwoAxisController sink);
		
	}
	
	public static class VerticalUpdateEvent extends UpdateEvent {

		private double vPos;
		
		public VerticalUpdateEvent(String name, double vPos) {
			super(name);
			this.vPos = vPos;
		}
		
		protected void updateController(TwoAxisController sink) {
			sink.vPos = vPos;
		}
	}
	
	public static class HorizontalUpdateEvent extends UpdateEvent {

		private double hPos;
		
		public HorizontalUpdateEvent(String name, double hPos) {
			super(name);
			this.hPos = hPos;
		}
		
		protected void updateController(TwoAxisController sink) {
			sink.hPos = hPos;
		}
	}

	public static class ActionUpdateEvent extends UpdateEvent {

		private boolean action;
		private int idx;
		
		public ActionUpdateEvent(String name, boolean action, int idx) {
			super(name);
			this.action = action;
		}
		
		protected void updateController(TwoAxisController sink) {
			sink.actions[idx] = action;
		}
	}
	
	
}
